<template>
  <view v-if="isShow">
    <u-tabbar :placeholder="true" :value="activeTabName" active-color="#264ea5">
      <u-tabbar-item
        v-for="tabbar of tabbarList"
        :key="tabbar.name"
        :text="tabbar.name"
        :icon="tabbar.icon"
        :name="tabbar.name"
        :badge="tabbar.name === '个人中心' ? count : null"
        @click="tabbarClick"
      ></u-tabbar-item>
    </u-tabbar>
  </view>
</template>

<script>
import ownerImg from "@/static/owner.jpg";
import ownerSelectedImg from "@/static/owner-selected.jpg";
import centerImg from "@/static/center.png";
import centerSelectedImg from "@/static/center-selected.png";
import patrolImg from "@/static/patrol.png";
import patrolSelectedImg from "@/static/patrol-selected.png";
import { editTaskByUuid, getTaskNotCompleteCount } from "@/api/task";
import { getNoteNotCompleteCount } from "@/api/note";
import { getUserByAll } from "@/api/user";
import { getVersion } from "@/api/version";

export default {
  name: "Tabbar",
  data() {
    return {
      isShow: false,
      interval: 0,
      count: 0,
      user: {},
      activeTabName: "",
      tabbarList: [
        {
          name: "物管巡逻",
          icon: patrolImg,
        },
        {
          name: "业主情况",
          icon: ownerImg,
        },
        {
          name: "个人中心",
          icon: centerImg,
        },
      ],
    };
  },
  mounted() {
    getVersion().then((res) => {
      let resIsReal = res.data.isReal;
      let envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
      let isReal = false;
      if (envVersion === "trial" && resIsReal) {
        isReal = true;
      } else if (envVersion === "trial" && !resIsReal) {
        isReal = false;
      } else if (envVersion !== "trial") {
        isReal = true;
      }
      this.isShow = isReal;
      if (!isReal) {
        return;
      }
      this.getUser().then(() => {
        this.getNotCompleteCount();
        this.interval = setInterval(async () => {
          await this.getNotCompleteCount();
        }, 5000);
      });
    });
  },
  beforeDestroy() {
    clearInterval(this.interval);
    this.interval = null;
  },
  methods: {
    async getUser() {
      let result = await getUserByAll();
      this.user = result.data.list.find((user) => {
        return user.name === this.$store.state.name;
      });
    },
    async getNotCompleteCount() {
      let taskResult = await getTaskNotCompleteCount({
        userUuid: this.user.uuid,
      });
      let taskCount = taskResult.data.count;
      let NoteResult = await getNoteNotCompleteCount({
        userUuid: this.user.uuid,
      });
      let noteCount = NoteResult.data.count;
      this.count = taskCount + noteCount;
      this.$emit("updateCount", { taskCount, noteCount });
    },
    async tabbarClick(event) {
      await this.setTabbarUrl(event);
    },
    async setTabbarStyle(name) {
      for (let i = 0; i < this.tabbarList.length; i++) {
        let tabbar = this.tabbarList[i];
        if (tabbar.name === "物管巡逻") {
          tabbar.icon = patrolImg;
        } else if (tabbar.name === "业主情况") {
          tabbar.icon = ownerImg;
        } else if (tabbar.name === "个人中心") {
          tabbar.icon = centerImg;
        }

        if (name === "物管巡逻" && tabbar.name === "物管巡逻") {
          tabbar.icon = patrolSelectedImg;
          this.activeTabName = name;
        } else if (name === "业主情况" && tabbar.name === "业主情况") {
          tabbar.icon = ownerSelectedImg;
          this.activeTabName = name;
        } else if (name === "个人中心" && tabbar.name === "个人中心") {
          tabbar.icon = centerSelectedImg;
          this.activeTabName = name;
        }
      }
    },
    async setTabbarUrl(name) {
      for (let i = 0; i < this.tabbarList.length; i++) {
        let tabbar = this.tabbarList[i];
        if (name === "物管巡逻" && tabbar.name === "物管巡逻") {
          let result = await uni.showModal({
            title: "确认提示",
            content: "确认开始巡逻",
          });
          if (result.confirm && !result.cancel) {
            await uni.reLaunch({
              url: "/pages/patrol/index",
            });
          }
        } else if (name === "业主情况" && tabbar.name === "业主情况") {
          if (this.activeTabName === "物管巡逻") {
            let result = await uni.showModal({
              title: "确认提示",
              content: "确认取消巡逻",
            });
            if (result.confirm && !result.cancel) {
              await uni.reLaunch({
                url: "/sub/owner/index",
              });
            }
          } else {
            await uni.reLaunch({
              url: "/sub/owner/index",
            });
          }
        } else if (name === "个人中心" && tabbar.name === "个人中心") {
          if (this.activeTabName === "物管巡逻") {
            let result = await uni.showModal({
              title: "确认提示",
              content: "确认取消巡逻",
            });
            if (result.confirm && !result.cancel) {
              await uni.reLaunch({
                url: "/pages/center/index",
              });
            }
          } else {
            await uni.reLaunch({
              url: "/pages/center/index",
            });
          }
        }
      }
    },
  },
};
</script>
